package com.cpi.basilos.user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import KC.ModSegApp.AplicacionApp;
import KC.ModSegApp.UsuarioApp;

import com.cpi.basilos.user.dto.UserDTO;
import com.cpi.basilos.user.exception.UserException;
import com.cs.common.exception.CSException;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;
import com.cs.common.pool.DBDAO;
import com.cs.common.pool.exception.PoolException;

/**
 * 
 * @description: This Class is used to handle the User Data Access
 * @author Edgar Joao
 */

public class UserDAO extends DBDAO{
	/**Application Module */
	private static final String MOD_APPLICATION = "MOD_SEG_APPLICATION";//$NON-NLS-1$
	/**Error invalid login/pass key */
	private static final String ERROR_INVALID_LOGIN_PASSWORD = "error.authentication.usertoappinvalid";//$NON-NLS-1$
	/**Error invalid login/app key */
	private static final String ERROR_INVALID_LOGIN_FOR_APP = "error.authentication.useropassinvalid";//$NON-NLS-1$
	
	private static final String SQL_SELECT_VALID_USER = "select almacen from VW_USUARIO_ALMACEN_VALIDO where login = ?";//$NON-NLS-1$
	/** Object for doing logs */
	private static org.apache.log4j.Logger logger = Logger.getLogger(UserDAO.class);
    /**
     * Login User method	
     * @param userDTO
     * @return UsuarioApp
     * @throws UserException
     */
	public UsuarioApp loginUser(UserDTO userDTO) throws UserException {
		Connection connection = null;
		UsuarioApp usuario = null;
		AplicacionApp aplicacion = null;
		SystemParameterCollection systemParameterCollection = null;
		SystemParameterBean systemParameterBean = null;
		String applicationName = null;
		try {
			connection = this.getConnection(dsModSeg);
			systemParameterCollection = SystemParameterCollection.getInstance();
			systemParameterBean = systemParameterCollection.getSystemParameterBean(MOD_APPLICATION);
			applicationName = systemParameterBean.getValue();
			usuario = UsuarioApp.usuarioValido(connection, userDTO.getUsrId(), userDTO.getPassword());
			if (null!=usuario) {
				aplicacion = AplicacionApp.aplicacionValida(connection, userDTO.getUsrId(), applicationName);
				if (null!=aplicacion) {
					usuario.setAplicacionAValidar(aplicacion);
				}else{
					UserException userException = null;
					userException = new UserException(ERROR_INVALID_LOGIN_PASSWORD,CSException.LAYER_DAO, CSException.ACTION_LOGIN);
					throw userException;
				}
			}else{
				UserException userException = null;
				userException = new UserException(ERROR_INVALID_LOGIN_FOR_APP,CSException.LAYER_DAO, CSException.ACTION_LOGIN);
				throw userException;
			}
		} catch (UserException userException){
			logger.error(userException);
			throw userException;
		} catch( PoolException poolException){
			UserException userException = null;
			logger.error(poolException);
			poolException.printStackTrace(System.err);
			userException = new UserException(poolException,CSException.LAYER_DAO, CSException.ACTION_LOGIN);
			throw userException;
		} catch (SQLException sqlException) {
			UserException userException = null;
			logger.error(sqlException);
			sqlException.printStackTrace(System.err);
			userException = new UserException(sqlException,CSException.LAYER_DAO, CSException.ACTION_LOGIN);
			throw userException;
		} catch (Exception exception) {
			UserException userException = null;
			logger.error(exception);
			exception.printStackTrace(System.err);
			userException = new UserException(exception,CSException.LAYER_DAO, CSException.ACTION_LOGIN);
			throw userException;
		}finally{
			try{
				closeConnection(connection);
			}catch(SQLException sqlException){
				logger.error(sqlException);
				sqlException.printStackTrace(System.err);
			}
		}
		return usuario;
	}
	
	public boolean validUserToApp(UserDTO userDTO) throws UserException {
		boolean validUser = false;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try{
			preparedStatement = prepareSQL(SQL_SELECT_VALID_USER, dsBasilos);
			preparedStatement.setString(1, userDTO.getUsrId());
			resultSet = executeQuery(preparedStatement);
			if( resultSet.next() ){
				userDTO.setWarehouseId(resultSet.getInt(1));
				validUser = true;
			}
			connection = preparedStatement.getConnection();
		}catch (Exception exception) {
			UserException userException = null;
			logger.error(exception);
			exception.printStackTrace(System.err);
			userException = new UserException(exception,CSException.LAYER_DAO, CSException.ACTION_LOGIN);
			throw userException;
		}finally{
			try{
				closeConnection(connection);
			}catch(SQLException sqlException){}
		}
		return validUser;
	}
}